package queryProccessor;

import java.util.ArrayList;
import java.util.StringTokenizer;

public class Select extends Command {
	String selectStatment;
	ArrayList<String> finalArray;
	
	protected String condition;
	private String[] columns;
	boolean result = true;
	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public String[] getColumns() {
		return columns;
	}

	public void setColumns(String[] columns) {
		this.columns = columns;
	}
	private final String[] ideal1 = { "SELECT", "columns", "FROM", "table_name" };
	private final String[] ideal2 = { "SELECT", "columns", "FROM",
			"table_name", "WHERE", "condition" };
	
	
	public Select(String s) {
		selectStatment = s;
		finalArray = new ArrayList<String>();
		convertToArray();
		result = checkSyntax();
	}

	@Override
	public void convertToArray() {
		int sel = search("SELECT ", selectStatment);
		int fr = search("FROM ", selectStatment);
		int wh = search("WHERE ", selectStatment);
		
		int sc = search(";", selectStatment);

		if (sel != -1 && fr != 1 && sc!=1) {
			finalArray.add("SELECT");
			String s = selectStatment.substring(sel + 6, fr);
			s = s.trim();
			finalArray.add(s);
			finalArray.add("FROM");
			 if (wh != -1) {
				s = selectStatment.substring(fr + 4, wh);
				s = s.trim();
				finalArray.add(s);
				finalArray.add("WHERE");
				s = selectStatment.substring(wh + 5, sc);
				s = s.trim();
				finalArray.add(s);
			} else {
				s = selectStatment.substring(fr + 4, sc);
				s = s.trim();
				finalArray.add(s);
			}
		}

	}

	@Override
	public boolean checkSyntax() {
		boolean result = true;
		if (finalArray.size() == 6) {
			int[] no = { 1, 3, 5 };
			if (result == lools(ideal2, no)) {
				table_name = finalArray.get(3);
				columns = divide(finalArray.get(1));
				condition = finalArray.get(5);

			}

		} else if (finalArray.size() == 4) {
			int[] no = { 1, 3 };
			if (result == lools(ideal1, no)) {
				table_name = finalArray.get(3);
				columns = divide(finalArray.get(1));

			}

		}

		else
			result = false;
		return result;
	}

	public boolean lools(String[] ideal, int[] no) {
		boolean result = true;
		for (int i = 0; i < finalArray.size(); i++) {
			if (!isTrue(i, no)) {
				if (!finalArray.get(i).equalsIgnoreCase(ideal[i])) {
					result = false;
					break;
				}
			}
		}
		return result;
	}
	private boolean isTrue(int x, int[] no) {
		for (int i = 0; i < no.length; i++) {
			if (no[i] == x)
				return true;
		}
		return false;
	}
	private String[] divide(String s) {

		StringTokenizer str = new StringTokenizer(s, ",");
		String[] result = new String[str.countTokens()];
		int i = 0;
		while (str.hasMoreTokens()) {
			result[i] = str.nextToken().trim();
			i++;
		}
		return result;
	}
//	public static void main(String[] args) {
//		Select n=new Select("SELECT C FROM T WHERE C<>2;");
//		for(int i=0;i<n.finalArray.size();i++){
//			System.out.println(n.finalArray.get(i));
//		}
//		
//		
//		System.out.println(n.getTable_name()+"/");
//		System.out.println(n.getCondition());
//		for(int j=0;j<n.getColumns().length;j++){
//			System.out.println(n.getColumns()[j]+":)");
//		}
//	}

	
}